package com.ky.manage.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.ky.manage.constant.OverallData;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;

public class SharedPreferencesUtil {
    private static final String CONFIG = "kykj";

    private static SharedPreferences sharedPreferences;

    // 存
    public static void saveFloatData(String key, float value) {
        if (sharedPreferences == null) {
            sharedPreferences = OverallData.app.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
        }

        sharedPreferences.edit().putFloat(key, value).commit();
    }

    // 取
    public static float getFloatData(String key, float defValue) {
        if (sharedPreferences == null) {
            sharedPreferences = OverallData.app.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
        }
        return sharedPreferences.getFloat(key, defValue);
    }

    public static int getIntData(String key, int defValue) {
        if (sharedPreferences == null) {
            sharedPreferences = OverallData.app.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
        }
        return sharedPreferences.getInt(key, defValue);
    }


    public static void saveIntData(String key, int value) {
        if (sharedPreferences == null) {
            sharedPreferences = OverallData.app.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
        }

        sharedPreferences.edit().putInt(key, value).commit();
    }

    // 存
    public static void saveBooleanData(String key, boolean value) {
        if (sharedPreferences == null) {
            sharedPreferences = OverallData.app.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
        }

        sharedPreferences.edit().putBoolean(key, value).commit();
    }

    // 取
    public static boolean getBooleanData(String key, boolean defValue) {
        if (sharedPreferences == null) {
            sharedPreferences = OverallData.app.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
        }
        return sharedPreferences.getBoolean(key, defValue);
    }

    //存
    public static void saveStringData(String key, String value) {
        if (sharedPreferences == null) {
            sharedPreferences = OverallData.app.getSharedPreferences(CONFIG,
                    Context.MODE_PRIVATE);
        }

       sharedPreferences.edit().putString(key, value).commit();
    }

    //自定义文件名取
    public static void saveStringDataForName(String fileName, String key, String value) {
        if (sharedPreferences == null) {
            sharedPreferences = OverallData.app.getSharedPreferences(fileName,
                    Context.MODE_PRIVATE);
        }

        sharedPreferences.edit().putString(key, value).commit();
    }

    // 取
    public static String getStringData(String key, String defValue) {
        if (sharedPreferences == null) {
            sharedPreferences = OverallData.app.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
        }
        return sharedPreferences.getString(key, defValue);
    }

    //自定义文件名
    public static String getStringDataForName(String fileName, String key, String defValue) {
        if (sharedPreferences == null) {
            sharedPreferences = OverallData.app.getSharedPreferences(fileName, Context.MODE_PRIVATE);
        }
        return sharedPreferences.getString(key, defValue);
    }

    /**
     * 序列化对象
     *
     * @param person
     * @return
     * @throws IOException
     */
    private static String serialize(Object person) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                byteArrayOutputStream);
        objectOutputStream.writeObject(person);
        String serStr = byteArrayOutputStream.toString("ISO-8859-1");
        serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
        objectOutputStream.close();
        byteArrayOutputStream.close();
        return serStr;
    }

    /**
     * 反序列化对象
     *
     * @param str
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private static Object deSerialization(String str) throws IOException,
            ClassNotFoundException {
        String redStr = java.net.URLDecoder.decode(str, "UTF-8");
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                redStr.getBytes(StandardCharsets.ISO_8859_1));
        ObjectInputStream objectInputStream = new ObjectInputStream(
                byteArrayInputStream);
        Object person = objectInputStream.readObject();
        objectInputStream.close();
        byteArrayInputStream.close();
        return person;
    }

    public static void saveObject(String key, Object object) throws IOException {
        if (TextUtils.isEmpty(key)) {
            return;
        }
        if (sharedPreferences == null) {
            sharedPreferences = OverallData.app.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(key, serialize(object));
        edit.commit();
    }

    public static Object getObject(String key) throws IOException, ClassNotFoundException {
        if (TextUtils.isEmpty(key)) {
            return "";
        }
        if (sharedPreferences == null) {
            sharedPreferences = OverallData.app.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
        }
        if (TextUtils.isEmpty(sharedPreferences.getString(key, ""))) {
            return null;
        }
        return deSerialization(sharedPreferences.getString(key, ""));
    }


}
